//
//  ViewController.m
//  ShanYanSDk
//
//  Created by 白红光 on 2018/5/29.
//  Copyright © 2018年 白红光. All rights reserved.
//

#import "ViewController.h"
#import <CL_ShanYanSDK/CLSecretLoginAndMachineCheckManager.h>
#import "OrdinaryLoginController.h"
#import "PhoneNumAuthenticationController.h"

#import <UIImage+GIF.h>
#import "NetWorkInfoManager.h"

@interface BasicInfo : NSObject

@property (nonatomic, copy) NSString * infoKey;
@property (nonatomic, strong) NSObject * infoValue;

@end

@implementation BasicInfo

@end

@interface ViewController ()

@property (nonatomic, strong) CLSecretLoginAndMachineCheckManager *secretLoginAndMach;


@property (nonatomic, assign) NSInteger flag;


@end

@implementation ViewController

- (CLSecretLoginAndMachineCheckManager *)secretLoginAndMach {
    
    if (_secretLoginAndMach == nil) {
        _secretLoginAndMach = [[CLSecretLoginAndMachineCheckManager alloc] init];
    }
    return _secretLoginAndMach;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    if (self.result.length == 11) {
        [self showMessage:[NSString stringWithFormat:@"登录成功！手机号为：%@",self.result]];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.title = @"闪验";
    
    [self setupUI];
}

// 快捷登录
- (void)quickLoginBtnClick {
    
    NSString *cellIP = [[NetWorkInfoManager sharedManager] getIpAddressCell];
    [self _addInfoWithKey:@"蜂窝地址" infoValue:cellIP];
    
    if (self.flag) {
        
         [self.secretLoginAndMach CMCCDatawithPageBlackgroundColr:nil withNavigationBlackgroundColor:nil withNavigationTitle:nil withTitleFont:0 withTitleColor:nil withAuthPageLogoImgOffsetY:nil withAuthPagelogoWidth:nil withAuthPagelogoHeight:nil withAuthPagelogButtonOffsetY:nil withMobileBorder:nil withMobileBorderHiden:nil withMobileNumColor:nil withMobileNumBKColor:nil withChangeNumHiden:nil withChangeNumColor:nil withLoginBtnBlackgroundColor:nil withLoginBtnTitle:nil withLoginBtnTitleColor:nil withPrivacyUserService:@"闪验产品协议" withPrivateTermTextOffsetY:nil withPrivateTermTextURL:@"https://www.baidu.com" withAuthLine:nil withBottomLabelClor:nil];
        
        [self.secretLoginAndMach getAllIdAndKeyWithCLAppid:@"e5N9vWQo" withCLAppKey:@"9JGvaDfL" withCLNav:self.navigationController withCLController:self withRequestType:@"mmdl" withIphoneNum:@"13120568228"];
        
        __weak typeof(self) weakself = self;
        self.secretLoginAndMach.resultApiKeyBlock = ^(NSString *resultApiKey) {
            NSLog(@"resultApiKey-----%@",resultApiKey);
        };
        
        self.secretLoginAndMach.resultmobileBlock = ^(NSDictionary *resultStr){
            
            NSLog(@"验证22：passedValue:%@",resultStr);
            NSString * code = resultStr[@"code"];
            if ([code isEqualToString:@"1000"]) {
                
                [weakself showMessage:[NSString stringWithFormat:@"登录成功！手机号为：%@",resultStr[@"mobile"]]];

            }else if ([code isEqualToString:@"1006"]){
                
                PhoneNumAuthenticationController * phonenumVC = [PhoneNumAuthenticationController new];
                phonenumVC.str = @"当前网络不稳定，已为您切换至手机号认证页面";
                [weakself.navigationController pushViewController:phonenumVC animated:YES];
                
            }else if ([code isEqualToString:@"1008"]){
                // 无SIM卡
                [weakself showMessage:resultStr[@"msg"]];
                
            }else {
                // 跳转验证码页面   1001  1002  1003  1004  1005  1007
                NSLog(@"验证码页面");
                
                OrdinaryLoginController * ordLoginVC = [OrdinaryLoginController new];
                ordLoginVC.str = @"当前网络不稳定，已为您切换至验证码登录页面";
                [weakself.navigationController pushViewController:ordLoginVC animated:YES];
                
            }
        };
    
    }else {
        [self showMessage:@"请打开移动网络"];
    }
    
}

- (void)setupUI {
    
    UILabel * topLabel = [[UILabel alloc]init];
    topLabel.textColor = RGBACOLOR(31,33,42,0.8);
    topLabel.text = @"传统验证太繁琐?";
    topLabel.font = [UIFont systemFontOfSize:28*WIDTHRADIUS];
    // 根据字体得到NSString的尺寸
    CGSize topSize = [topLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil]];
    CGFloat topH = topSize.height;
    [self.view addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.offset(70*WIDTHRADIUS);
        make.height.offset(topH);
        
    }];
    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"homeGIF" ofType:@"gif"];
    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
    UIImage *image = [UIImage sd_animatedGIFWithData:imageData];

    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    imageView.image = image;
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(topLabel.mas_bottom).offset(23*WIDTHRADIUS);
        make.left.right.offset(0);
        make.height.offset(338*WIDTHRADIUS);
    }];
    
    UIImageView * gifbottomImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gifbottomimg"]];
    [self.view addSubview:gifbottomImg];
    [gifbottomImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.offset(0);
        make.top.equalTo(imageView.mas_bottom);
        make.height.offset(53*WIDTHRADIUS);
        
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = RGBCOLOR(31,33,42);
    titleLabel.text = @"试试闪验!";
    titleLabel.font = [UIFont systemFontOfSize:28*WIDTHRADIUS];
    
    CGSize titleSize = [titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil]];
    CGFloat nameH = titleSize.height;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(gifbottomImg.mas_bottom).offset(0*WIDTHRADIUS);
        make.height.offset(nameH);
        
    }];
    
    UIButton * quickLoginBtn = [UIButton new];
    [quickLoginBtn setTitle:@"立即体验" forState:UIControlStateNormal];
    [quickLoginBtn.titleLabel setFont:[UIFont systemFontOfSize:18*WIDTHRADIUS]];
    [quickLoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [quickLoginBtn setBackgroundColor:RGBCOLOR(78,132,255)];
    [self.view addSubview:quickLoginBtn];
    [quickLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(titleLabel.mas_bottom).offset(31*WIDTHRADIUS);
        make.width.offset(331*WIDTHRADIUS);
        make.height.offset(55*WIDTHRADIUS);
        
    }];
    
    [quickLoginBtn addTarget:self action:@selector(quickLoginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [quickLoginBtn.layer setMasksToBounds:YES];
    [quickLoginBtn.layer setCornerRadius:27.5*WIDTHRADIUS];
    
}

- (void)_addInfoWithKey:(NSString *)infoKey infoValue:(NSObject *)infoValue {
    BasicInfo *info = [[BasicInfo alloc] init];
    info.infoKey = infoKey;
    info.infoValue = infoValue;
    NSLog(@"%@---%@", infoKey, infoValue);
    if ([infoKey isEqualToString:@"蜂窝地址"]) {
        
        if ([[NSString stringWithFormat:@"%@",infoValue] isEqualToString:@"该设备不存在该ip地址"]) {
            
            self.flag = 0;
        }else {
            
            self.flag = 1;
        }
    }
}

// 吐丝
- (void)showMessage:(NSString *)msg {
    if ([msg isEqualToString:@""]) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = [UIFont systemFontOfSize:18.f];
    hud.detailsLabel.backgroundColor = [UIColor clearColor];
    hud.margin = 15;
    hud.backgroundView.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2.0];
    
}

@end
